﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeGenerator.Interface;
using System.Data;
using System.IO;

namespace CodeGenerator.Control
{
    class CCreatEntityClass : ICreatEntityClass
    {
        #region 实体类代码编制
        /// <summary>
        /// 创建实体类
        /// </summary>
        /// <param name="dt">字段集合</param>
        /// <param name="tableName">表名</param>
        /// <param name="className">类名</param>
        /// <param name="classNameSpace">命名空间</param>
        /// <returns>实体类的代码内容字符</returns>
        public string CreatEntityClass(string tableName, DataTable dt, string className, string classNameSpace)
        {
            StringBuilder strBuilder = new StringBuilder();
            //文件头注释
            strBuilder.AppendLine("// ================================================================================");
            strBuilder.AppendLine("// File: " + className + ".cs");
            strBuilder.AppendLine("// Desc: 表[" + tableName + "]的实体类");
            strBuilder.AppendLine("// 此实体类通过代码生成工具（EntityBuilder）自动生成。");
            strBuilder.AppendLine("// Auth: Gavin");
            strBuilder.AppendLine("// Date: " + DateTime.Now.ToString("yyyy年MM月dd日"));
            strBuilder.AppendLine("// ================================================================================");
            strBuilder.AppendLine("// Change History");
            strBuilder.AppendLine("// ================================================================================");
            strBuilder.AppendLine("// 		Date:		Author:				Description:");
            strBuilder.AppendLine("// 		--------	--------			-------------------");
            strBuilder.AppendLine("//    ");
            strBuilder.AppendLine("// ================================================================================");
            strBuilder.AppendLine("// Copyright Really Soft© 2011");
            strBuilder.AppendLine("// ================================================================================");
            strBuilder.AppendLine();

            //Using
            strBuilder.AppendLine("using System;");
            strBuilder.AppendLine("using System.Data;");
            strBuilder.AppendLine();

            //namespace
            strBuilder.AppendLine("namespace " + classNameSpace);
            strBuilder.AppendLine("{");

            //class desc
            strBuilder.AppendLine("    /// <summary>");
            strBuilder.AppendLine("    /// 表[" + tableName + "]的实体类");
            strBuilder.AppendLine("    /// </summary>");

            //class
            strBuilder.AppendLine("    public class " + className);
            strBuilder.AppendLine("    {");

            //字段属性
            strBuilder.AppendLine("        #region 表字段属性");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strBuilder.AppendLine("        private " + ConvertSystypesToCSharpType(dt.Rows[i][1].ToString()) + " _" + dt.Rows[i][0].ToString() + ";");
                strBuilder.AppendLine("        public " + ConvertSystypesToCSharpType(dt.Rows[i][1].ToString()) + " " + dt.Rows[i][0].ToString());
                strBuilder.AppendLine("        {");
                strBuilder.AppendLine("            get");
                strBuilder.AppendLine("            {");
                strBuilder.AppendLine("                return" + " _" + dt.Rows[i][0].ToString() + ";");
                strBuilder.AppendLine("            }");
                strBuilder.AppendLine("            set");
                strBuilder.AppendLine("            {");
                strBuilder.AppendLine("                " + "_" + dt.Rows[i][0].ToString() + " = value;");
                strBuilder.AppendLine("            }");
                strBuilder.AppendLine("        }");
                strBuilder.AppendLine();
            }
            strBuilder.AppendLine("        #endregion");
            strBuilder.AppendLine();

            strBuilder.AppendLine("    }");
            strBuilder.AppendLine("}");

            return strBuilder.ToString();
        }
        /// <summary>
        /// 数据库数据类型转换为C#数据类型
        /// </summary>
        /// <param name="types">数据库数据类型</param>
        /// <returns>C#数据类型</returns>
        private string ConvertSystypesToCSharpType(string types)
        {
            string reval = string.Empty;
            switch (types.ToLower())
            {
                case "int":
                    reval = "Int32";
                    break;
                case "text":
                    reval = "string";
                    break;
                case "bigint":
                    reval = "Int64";
                    break;
                case "binary":
                    reval = "byte[]";
                    break;
                case "bit":
                    reval = "bool";
                    break;
                case "char":
                    reval = "string";
                    break;
                case "datetime":
                    reval = "DateTime";
                    break;
                case "decimal":
                    reval = "decimal";
                    break;
                case "float":
                    reval = "double";
                    break;
                case "image":
                    reval = "byte[]";
                    break;
                case "money":
                    reval = "decimal";
                    break;
                case "nchar":
                    reval = "string";
                    break;
                case "ntext":
                    reval = "string";
                    break;
                case "numeric":
                    reval = "decimal";
                    break;
                case "nvarchar":
                    reval = "string";
                    break;
                case "real":
                    reval = "Single";
                    break;
                case "smalldatetime":
                    reval = "DateTime";
                    break;
                case "smallint":
                    reval = "Int16";
                    break;
                case "smallmoney":
                    reval = "decimal";
                    break;
                case "timestamp":
                    reval = "DateTime";
                    break;
                case "tinyint":
                    reval = "byte";
                    break;
                case "uniqueidentifier":
                    reval = "guid";
                    break;
                case "varbinary":
                    reval = "byte[]";
                    break;
                case "varchar":
                    reval = "string";
                    break;
                case "variant":
                    reval = "object";
                    break;
                default:
                    reval = "string";
                    break;
            }
            return reval;
        }
        #endregion

        #region 将代码写入.CS文件

        /// <summary>
        /// 将生成的代码写入.CS文件中
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="strCode">代码文件</param>
        /// <param name="filePath">存储路径</param>
        /// <returns>写入是否成功</returns>
        public bool CreatEntityClassFile(string className, string strCode, string filePath)
        {
            filePath += className + ".cs";
            FileInfo info = new FileInfo(filePath);
            if (!info.Directory.Exists)
            {
                Directory.CreateDirectory(info.DirectoryName);
            }
            StreamWriter stream = null;
            //保存
            try
            {
                stream = new StreamWriter(filePath, false, Encoding.Default);
                stream.Write(strCode);
                return true;
            }
            catch
            {
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }

        #endregion
    }
}
